package com.young.nyd.web;

import com.young.common.core.excel.ExcelExport;
import com.young.common.core.excel.ExcelImport;
import com.young.common.core.excel.IExcelExport;
import com.young.common.core.excel.IExcelImport;
import com.young.common.util.CommonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.nyd.model.UviListhazardous;
import com.young.nyd.model.UviPollutiontreatment;
import com.young.nyd.model.UviSuperviseenterprises;
import com.young.nyd.service.IUviListhazardousService;
import com.young.nyd.service.IUviSuperviseenterprisesService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* 有害物质清单服务
* Created by  on 2018-10-19.
*/
@Controller
@RequestMapping("/nyd/uviListhazardous")
public class UviListhazardousController {

    @Resource(name="uviListhazardousService")
    IUviListhazardousService service;

    @Resource(name="uviSuperviseenterprisesService")
    IUviSuperviseenterprisesService enterpriseService;

    /**
     * 获取数据列表
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询有害物质清单列表")
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(UviListhazardous obj, HttpServletRequest request) throws Exception{
        List<UviListhazardous> list = service.getList(obj);
        return new Result(list);
    }

    /**
     * 获取分页数据
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("分页查询有害物质清单列表")
    @RequestMapping("/getPage")
    @ResponseBody
    public Object getPage(UviListhazardous obj, HttpServletRequest request) throws Exception{
        Page page = CommonUtil.getPageFromRequest(request);
        List<UviListhazardous> list = service.getPage(obj, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param harmfullistId
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询有害物质清单")
    @RequestMapping("/get")
    @ResponseBody
    public Object get(String harmfullistId, HttpServletRequest request) throws Exception{
        UviListhazardous obj = service.get(harmfullistId);
        return new Result(obj);
    }

    /**
     * 统计数量
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("统计有害物质清单数量")
    @RequestMapping("/count")
    @ResponseBody
    public Object count(UviListhazardous obj, HttpServletRequest request) throws Exception{
        return new Result(service.count(obj));
    }

    /**
     * 新增
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("新增有害物质清单")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(UviListhazardous obj, HttpServletRequest request) throws Exception{
        UviSuperviseenterprises e = enterpriseService.get(obj.getEnterpriseCode().toString());
        if(e!=null){
            service.insert(obj);
            return new Result();
        }else{
            return new Result(1,"没有关联重点监管企业，无法添加！");
        }

    }

    /**
     * 修改
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改有害物质清单")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(UviListhazardous obj, HttpServletRequest request) throws Exception{
        if(obj.getUnlack().equals("0")){
            service.update(obj);
            return new Result();
        }else{
            return new Result(-1,"信息已锁定，请联系省级管理员解除锁定！方能修改");
        }
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("删除有害物质清单")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            service.delete(idArr);
        }else if(StringUtils.isNotBlank(id)){
            service.delete(id);
        }else{
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

    /**
     * 锁定
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("锁定有害物质清单")
    @RequestMapping("/lock")
    @ResponseBody
    public Object lock(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        UviListhazardous s = new UviListhazardous();
        int i = 0;
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            for(String id1 : idArr){
                s.setHarmfullistId(id1);
                s.setUnlack("1");
                i += service.update(s);
            }
            if(i>=1){
                result.setCode(0);
            }else{
                result.setCode(-1);
                result.setInfo("锁定失败:待锁定编号无效!");
            }
        }else if(StringUtils.isNotBlank(id)){
            s.setHarmfullistId(id);
            s.setUnlack("1");
            i = service.update(s);
            if(i>=1){
                result.setCode(0);
            }else{
                result.setCode(-1);
                result.setInfo("锁定失败:待锁定编号无效!");
            }
        }else{
            result.setCode(-1);
            result.setInfo("锁定失败:待锁定编号无效!");
        }
        return result;
    }

    /**
     * 解锁
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("解锁有害物质清单")
    @RequestMapping("/unlock")
    @ResponseBody
    public Object unlock(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        UviListhazardous s = new UviListhazardous();
        int i = 0;
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            for(String id1 : idArr){
                s.setHarmfullistId(id1);
                s.setUnlack("0");
                i += service.update(s);
            }
            if(i>=1){
                result.setCode(0);
            }else{
                result.setCode(-1);
                result.setInfo("解锁失败:待解锁编号无效!");
            }
        }else if(StringUtils.isNotBlank(id)){
            s.setHarmfullistId(id);
            s.setUnlack("0");
            i = service.update(s);
            if(i>=1){
                result.setCode(0);
            }else{
                result.setCode(-1);
                result.setInfo("解锁失败:待解锁编号无效!");
            }
        }else{
            result.setCode(-1);
            result.setInfo("解锁失败:待解锁编号无效!");
        }
        return result;
    }

    /**
     * 导入
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @Log("导入有害物质清单")
    @RequestMapping("/import")
    @ResponseBody
    public Object importExcel(MultipartFile file, HttpServletRequest request) throws Exception{
        Result result = new Result();
        try {
            if (file != null && !file.isEmpty()) {
                //excel导入处理,返回excel中的数据集合
                IExcelImport ei = new ExcelImport(file);//将文件转为ExcelImport对象
                //从excel读取数据
                List<UviListhazardous> list = ei.getImportDataAsBean(UviListhazardous.class);
                if (list != null && list.size() > 0){
                    int num = service.batchInsert(list);//批量插入
                    result.setInfo("成功导入数据" + num + "条!");
                }else {
                    result.setCode(-1);
                    result.setInfo("导入失败:excel解析后结果为空!");
                }
            } else {
                result.setCode(-1);
                result.setInfo("导入失败:文件为空!");
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        }
        return result;
    }

    /**
     * 导出
     * @param obj 查询参数
     * @param request
     * @param response
     * @throws Exception
     */
    @Log("导出有害物质清单")
    @RequestMapping("/export")
    public void exportExcel(UviListhazardous obj, HttpServletRequest request, HttpServletResponse response,String ids) throws Exception {
        List<UviListhazardous> list = new ArrayList<UviListhazardous>();
        if(ids != null && !"".equals(ids)){
            String[] ids1 = ids.split(",");
            for (String id : ids1){
                UviListhazardous u =  service.get(id);
                list.add(u);
            }
        }else{
            list = service.getList(obj);
        }


        if (list != null && list.size() > 0){
            IExcelExport ee = new ExcelExport();
            ee.insertBeanList(list, UviListhazardous.class);
            ee.write2Response(response, "有害物质清单_" + System.currentTimeMillis() + ".xls");
        }else{
            response.getWriter().write("数据为空!");
        }
    }

}